﻿namespace Reading
{
    using System;
    using System.Collections;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using Microsoft.Phone.Controls;
    using Reading.Controls;
    using Reading.ReadingEx;

    public partial class ReadingPage : PhoneApplicationPage
    {
        ReadingHandler handler;

        ScrollViewer[] viewers;

        ContentControl[] viewerContents;

        int index = 1;

        double y, x;

        bool canNext, canPrevious;
        
        // 构造函数
        public ReadingPage()
        {
            InitializeComponent();

            viewers = new ScrollViewer[3];
            viewerContents = new ContentControl[3];
            Items = new Collection<object>();
            selectedIndex = -1;

            for (int i = 0; i < ItemsContainer.Children.Count; i++)
            {
                ScrollViewer viewer = ItemsContainer.Children[i] as ScrollViewer;

                // assigned width.
                viewer.Width = Application.Current.Host.Content.ActualWidth;
                viewerContents[i] = (viewer.Content as StackPanel).Children[0] as ContentControl;
                viewers[i] = viewer;
            }

            Loaded += OnPageLoaded;
        }

        #region events handlers
        void OnPageLoaded(object sender, RoutedEventArgs e)
        {
            handler = new ReadingHandler();
        }

        void OnTouchFrameReported(object sender, TouchFrameEventArgs e)
        {
            var ps = e.GetTouchPoints(this.ItemsContainer);
            if (ps.Count > 0)
            {
                var p = ps[0];

                if (p.Action == TouchAction.Down)
                {
                    y = p.Position.Y;
                    x = p.Position.X;
                }

                if (p.Action == TouchAction.Up)
                {
                    if (Math.Abs(p.Position.Y - y) < 300)
                    {
                        if (p.Position.X - x > 100)
                        {
                            ChangeToPrevious();
                        }
                        else if (p.Position.X - x < -100)
                        {
                            ChangeToNext();
                        }
                    }
                }
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            Touch.FrameReported += OnTouchFrameReported;
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            Touch.FrameReported -= OnTouchFrameReported;
        }
        #endregion

        int selectedIndex;
        public int SelectedIndex
        {
            get { return selectedIndex; }
            private set
            {
                if (value != selectedIndex)
                {
                    var old = selectedIndex;
                    selectedIndex = value;

                    if (selectedIndex > 0)
                    {
                        canPrevious = true;
                    }
                    else
                    {
                        canPrevious = false;
                    }

                    if (selectedIndex < handler.Count - 1)
                    {
                        canNext = true;
                    }
                    else
                    {
                        canNext = false;
                    }
                }
            }
        }

        void ChangeToNext()
        {
            if (!canNext) { return; }

            int rIndex = ((index + 1) % 3);
            int lIndex = (index % 3);

            Storyboards ss = Storyboards.CreateRightToLeft(viewers[rIndex], viewers[lIndex]);
            ss.Completed += (s, e) => viewers[lIndex].ScrollToVerticalOffset(0);
            ss.Begin();

            index = (++index) % 3;
            System.Diagnostics.Debug.WriteLine(string.Format("index:{0}, {1} <- {2}", index, lIndex, rIndex));

            // update items
            SelectedIndex++;
            if (SelectedIndex < handler.Count - 1)
            {
                int next = (index + 1) % 3;
                viewerContents[next].DataContext = handler.GetContent(SelectedIndex + 1);
            }
        }

        void ChangeToPrevious()
        {
            if (!canPrevious) { return; }

            int lIndex = (index - 1 % 3);
            int rIndex = (index % 3);
            if (lIndex < 0) { lIndex += 3; }

            Storyboards ss = Storyboards.CreateLeftToRight(viewers[lIndex], viewers[rIndex]);
            ss.Completed += (s, e) => viewers[rIndex].ScrollToVerticalOffset(0);
            ss.Begin();

            index--;
            if (index < 0) { index += 3; }
            System.Diagnostics.Debug.WriteLine(string.Format("index:{0}, {1} -> {2}", index, lIndex, rIndex));

            // update items
            SelectedIndex--;
            if (SelectedIndex > 0)
            {
                int previous = (index - 1) % 3;
                if (previous < 0)
                {
                    previous += 3;
                }
                viewerContents[previous].DataContext = handler.GetContent(SelectedIndex - 1);
            }
        }
    }
}